﻿using AnesSystem.Services;
using AnesSystem.ViewModels.Operation.CQ.RCQRMYY;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls.Operation.CQ.RCQRMYY
{
    /// <summary>
    /// AnethOperInfoYiShissCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class AnethOperInfoYiShissCtrl : UserControl
    {
      
        public AnethOperInfoYiShissCtrl()
        {
            InitializeComponent();
           
            this.Loaded += AnethOperInfoYiShissCtrl_Loaded;
        }

        private void AnethOperInfoYiShissCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            InitData();

        }


        #region Properties


        #endregion

        #region 依赖属性       

        #endregion

        #region Event



        #endregion

        #region Public/Protected Methods
        private void SetCheckBoxChecked()
        {
            string s = Regex.Replace(txtArea.Text.Trim(), @"\d、", "*");
            string[] textlist = s.Split('*');
            List<CheckBox> listCtrl = Common.UIHelper.FindVisualChild<CheckBox>(_sp);
            foreach (var item in listCtrl)
            {
                item.Checked -= CheckBox_Checked;
                item.Unchecked -= CheckBox_Checked;
                if (textlist.Contains(item.Content))
                {
                    item.IsChecked = true;
                }
                else
                {
                    item.IsChecked = false;
                }
                item.Checked += CheckBox_Checked;
                item.Unchecked += CheckBox_Checked;
            }
            //SetTextAreaConent();
        }

        private void SetTextAreaConent()
        {
            string s = Regex.Replace(txtArea.Text.Trim(), @"\d\、", "*");
            string[] textlist = s.Split('*');
            List<CheckBox> listCtrl = Common.UIHelper.FindVisualChild<CheckBox>(_sp);
            List<string> tempStr = new List<string>();
            foreach (var item in textlist)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    tempStr.Add(item);
                }
            }

            foreach (CheckBox cb in listCtrl)
            {
                if ((bool)cb.IsChecked && !tempStr.Contains(cb.Content.ToString()))
                {
                    tempStr.Add(cb.Content.ToString());
                    break;
                }
                else if (!(bool)cb.IsChecked && tempStr.Contains(cb.Content.ToString()))
                {
                    tempStr.Remove(cb.Content.ToString());
                    break;
                }
            }
            string txt = "";
            for (int i = 1; i <= tempStr.Count; i++)
            {
                txt += i + "、" + tempStr[i - 1];
            }
            txtArea.Text = txt;
        }

        private void InitData()
        {
            if (DataManager.Instance.CurrentPatientLink == null) return;
            txtArea.Text = DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Ysss1;
        }

        #endregion

        #region Public/Protected Methods


        #endregion

        #region EventHandlers

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string commond = (sender as Button).Tag.ToString();
            switch (commond)
            {
                case "Save":
                    {
                        DataManager.Instance.IsNotNeedSaveData = false;
                        DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Ysss1 = txtArea.Text;
                        DrugAndEventWindow.Instance.CloseWin();
                    }
                    break;
                case "Cancel":
                    {
                        DrugAndEventWindow.Instance.CloseWin();
                    }
                    break;
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            txtArea.TextChanged -= TextBox_TextChanged;
            SetTextAreaConent();
            txtArea.TextChanged += TextBox_TextChanged;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            SetCheckBoxChecked();
        }
       

        #endregion
    }


}


